﻿

//function saveMultipleConditions($cw,$target) {
//    var $root = $cw.find('#condition-tree').children('ul').children('li');
//    var data = getDataByLi($root);
//    data.NodeData = { container: '$$' }; //给根目录赋值一个与运算
//    //$newnode.data('av', data);
//    $cw.data('tWindow').close();
//}

function onOthersDrop(e) {
    var $target = $(e.dropTarget);

    if ($target.is('ul.ws-queues') || $target.is('ul.ws-containers')) {
        var $item = $(e.item);
        var $newli;

        if ($item.hasClass('dt-queue')) { //拖拽队列
            $newli = $('.ws-queues').addListItem(null);
            $newli.addHoverFunctions({ deleteEvent: function () {
                $newli.remove();
            }
            });
        }
        else if ($item.hasClass('dt-container')) { //拖拽环节容器
            var $acul = $target.find('ul.ws-containers');
            $('.ws-containers').sortable();
            $newli = $target.addListItem(null);
            addFuntionsToOther($newli);
        }
        e.preventDefault();
    }
}

//环节容器多条件编辑
function addFuntionsToOther($newnode) {
    var $cw = $('#w-condition');
    $cw.find('.condition-element').sortable();
    $newnode.addHoverFunctions({
        deleteEvent: function () {
            if (confirm('确定删除？'))
                $newnode.remove();
        },
        detailEvent: function () {
            var $target = $(this);
            var $root = $("#condition-tree").children('ul').children('li');
            var $group = $root.find('ul').first();
            if ($group.length == 0) {
                $('<span class="t-icon t-plus"></span>').prependTo($root);
                $group = $('<ul class="t-group"></ul>').appendTo($root);
            }
            $('#condition-tree').droppable(
            {
                accept: 'li.conditiondesign',
                drop: function (event, ui) {
                    var elementtype = ui.draggable.attr('elementtype');
                    var $newli = $('<li controltype="text" class="t-item"><div class="t-mid"><span class="t-in">' + ui.draggable.text() + '</span></div></li>');
                    $newli.attr('elementtype', elementtype);
                    var data = ui.draggable.data('lidata');
                    $newli.data('lidata', data);

                    $group.children().last().removeClass('t-last').children().first().removeClass('t-bot').addClass('t-mid');
                    $newli.appendTo($group).addClass('t-last').children().first().addClass('t-bot');

                }
            });
            //初始化运算符数据
            var containerid = $newnode.attr('id');
            $cw.find('ul.condition-element').first().find("li").each(function () {
                var $item = $(this);
                if ($item.attr('elementtype') == 'condition-and') {
                    $item.data('lidata', { Condition: {}, Operator: '&&', ChildrenData: new Array(), ProcessID: processid, ContainerID: containerid, ID: null });
                }
                if ($item.attr('elementtype') == 'condition-or') {
                    $item.data('lidata', { Condition: {}, Operator: '||', ChildrenData: new Array(), ProcessID: processid, ContainerID: containerid, ID: null });
                }
            });

            //初始化条件池
            var $condtionpool = $cw.find('ul.condition-element:eq(1)').children().remove().end();
            var $definedconditions = $('.ws-conditions').find('li');
            $definedconditions.each(function () {
                var $dcitem = $(this);
                var dcdata = $dcitem.data('lidata');
                var c = { Condition: dcdata, Operator: null, ChildrenData: new Array(), ContainerID: containerid, ProcessID: processid, ID: null }; //node的对应data
                var $newli = $('<li class="conditiondesign"><div class="text-display"></div></li>').data('lidata', c).find('.text-display').text(dcdata.ConditionName + ":" + dcdata.Field + " " + dcdata.ConditionOperator + " " + dcdata.ConditionValue)
                  .end().appendTo($condtionpool).attr('elementtype', 'condition-single');
            });

            //初始化条件树
            var $ul = $('#condition-tree').children('ul').children('li').children('ul');
            $ul.children().remove();
            var nodedatas = $newnode.data('nodedatas');
            if (nodedatas != null) {
                var $li = getLiByData(nodedatas);
                $li.appendTo($ul);

                //修改树连接线
                $('#condition-tree').find('ul').each(function () {
                    $(this).children('li').first().children('div').addClass('t-top')
                });
                $('#condition-tree').find('ul').each(function () {
                    $(this).children('li').last().addClass('t-last').children('div').addClass('t-bot');
                });

                $ul.find('li').each(function () {
                    if ($(this).children('ul').length > 0) {
                        $(this).prepend('<span class="t-icon t-minus"></span>');
                    }
                });
            }

            //初始化功能按钮
            $div = $('.multiplecondition');
            $div.find('.btns').remove();
            var $btns = $('<center class="btns"><input type="button" class="savebtn" value="保存" /><input type="button" class="cancelbtn" value="取消" /></center>').appendTo($div);
            $btns.find('.savebtn').click(function () {
                var $root = $cw.find('#condition-tree').children('ul').children('li');

                olddata = $root.children('ul').children('li').data('lidata');
                if (olddata) {
                    olddata.ChildrenData = new Array();
                    $root.children('ul').children('li').data('lidata', olddata);
                }
                var gdata = getDataByLi($root.children('ul').children('li'));
                $newnode.data('nodedatas', gdata);
                $cw.data('tWindow').close();
            });
            $btns.find('.cancelbtn').click(function () {
                $cw.data('tWindow').close();
            });

            var $screen = $(document);
            $cw.css('left', $screen.width() / 2 - $('#w-condition').width() / 2).css('top', '150px').data('tWindow').title('表单编辑框').open();
        },
        valign: 'top'
    });
}

function getLiByData(data) {
    var $newli =$('<li controltype="text" class="t-item"><div class="t-mid"><span class="t-in"></span></div></li>');

    if (data.Operator == '&&') {
        $newli.attr('elementtype', 'condition-and');
        $newli.find('span').text("并运算(AND)");
    }
    else if (data.Operator == '||') {
        $newli.attr('elementtype', 'condition-or');
        $newli.find('span').text("或运算(OR)");
    }
    else if (data.Condition) {
        var text = data.Condition.ConditionName + ":" + data.Condition.Field + " " + data.Condition.ConditionOperator + " " + data.Condition.ConditionValue;
        $newli.find('span').text(text);
    }


    if (data.ChildrenData && data.ChildrenData.length > 0) {
        var $childul = $('<ul class="t-group"></ul>').appendTo($newli);

        for (var i = 0; i < data.ChildrenData.length; i++) {
            var $childLi = getLiByData(data.ChildrenData[i]);
            $childLi.appendTo($childul);
        }
    }
    $newli.data('lidata', data);
    return $newli;
}

function getDataByLi($li) {
    var data = $li.data('lidata');
    data.ChildrenData = new Array();
    var $ul = $li.children('ul');
    var $lichildren = $ul.children('li');
    $lichildren.each(function () {
       var childNodeData = getDataByLi($(this));
       childNodeData.ParentNodeDataID = data.id;
       data.ChildrenData.push(childNodeData);
   });
    return data;
}

function getCondtainersData() {
    var queues = new Array();
    $('ul.ws-queues').children('li').each(function () {
        var queueliid = $(this).attr('id');
        var containers = new Array();
        $(this).find('ul.ws-containers').children('li').each(function () {
            var $containeritem = $(this);
            var id = $containeritem.attr('id');
            var actarray = new Array();
            var activities = $containeritem.find('li').each(function () {
                var ad = $(this).data('lidata');
                ad.ProcessID = processid;
                ad.ContainerID = id;
                actarray.push(ad);
            });
            var nds = $containeritem.data('nodedatas');

            var root = new Array();
            root.push(nds == null ? null : nds);
            containers.push({ NodeDatas: root, QueueID: queueliid, ProcessID: processid, ID: id,ContainerID:id, Activities: actarray });
        });

        queues.push({ ProcessID: $('#processid').val(), ID: $(this).attr('id'), Containers: containers });
    });
    return queues;
}

function setContainerData(data) {
    if (data) {
        var $ul = $('.ws-queues');
        for (var i = 0; i < data.length; i++) {
            var $li = $ul.addListItem({ lidata: data[i] });
            initialQueueLi($li);
            $li.addHoverFunctions({ deleteEvent: function () {
                $li.remove();
            }
            });
            $li.attr('id', data[i].id);
        }
    }
}

function initialQueueLi($li) {
    var $ul = $li.find('ul.ws-containers');
    var data = $li.data('lidata');
    if (data.Containers) {
        for (var i = 0; i < data.Containers.length; i++) {
            var $contianerli = $ul.addListItem({ lidata: data.Containers[i] });
            initialContainerLi($contianerli);
            $contianerli.attr('id', data.Containers[i].id);
        }
    }
}

function initialContainerLi($li) {
    addFuntionsToOther($li);
    var lidata = $li.data('lidata');
    $li.data('nodedatas', lidata.NodeDatas[0]);
    var $aul = $li.find('ul');
    for (var i = 0; i < lidata.Activities.length; i++) {
        var $newa = $aul.addListItem({ lidata: lidata.Activities[i] });
        initialActivityLi($newa, lidata.Activities[i].ActivityName);
    }
    
}